package com.example.studentmanagmentsystem;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.Operator;

import java.util.List;

public class StudentShowActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_show);
        //取出学生、课程与成绩信息
        List<Student> students = LitePal.findAll(Student.class);
        List<Course> courses = LitePal.findAll(Course.class);
        List<Score> scores = LitePal.findAll(Score.class);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_student);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        StudentAdapter adapter = new StudentAdapter(students);

        adapter.setOnDeleteClickListener(new StudentAdapter.OnDeleteClickListener() {
            @Override
            public void onDeleteClick(View view, int position) {

                AlertDialog.Builder builder = new AlertDialog.Builder(StudentShowActivity.this);
                builder.setTitle("删除学生成绩信息").setMessage("你确定要删除吗?").setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Operator.deleteAll(Student.class, "id = ?", students.get(position).getId() + "");
                        Toast.makeText(StudentShowActivity.this, "成功删除该同学信息 =>" + students.get(position).getName(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(StudentShowActivity.this, StudentShowActivity.class);
                        startActivity(intent);
                    }
                });
                builder.show();

            }
        });

        adapter.setOnQueryClickListener(new StudentAdapter.OnQueryClickListener() {
            @Override
            public void onQueryClick(View view, int position) {

                String studentName = students.get(position).getName();
                Intent intent = new Intent(StudentShowActivity.this, ScoreShowActivity.class);
                intent.putExtra("StudentName", studentName);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);


    }
}